/**
  * Author: Jordi Vilaplana Mayoral <jvilaplana@semicinternet.com>
  * Date: 16/05/2011
  * 
  * OpenFileRename
  * Copyright (C) 2011 Semic Internet
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  * 
  * @(#)
  * Description: Renames files following the specified format in Alfresco.
  */

package model;

import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class OpenFileModel {
	private Properties configFile;
	private String sLanguage;
	private Locale locale;
	
	public OpenFileModel() {
		configFile = loadConfiguration();
	}
	
	public void setLanguage(String sLanguage) {
		configFile.setProperty("defaultLanguage", sLanguage);
		try {
			configFile.store(new FileOutputStream(new File("openfile.properties")), "OpenLogViewer configuration file");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void open(URI uri) {
        if (Desktop.isDesktopSupported()) {
                Desktop desktop = Desktop.getDesktop();
                try {
                        desktop.browse(uri);
                } catch (IOException e) {
                }
        } else {
        }
    }

	public Properties loadConfiguration() {
		try {
			configFile = new Properties();
			configFile.load(new FileInputStream("openfile.properties"));
			sLanguage = configFile.getProperty("defaultLanguage");
		}
		catch (FileNotFoundException e) { e.printStackTrace(); }
		catch (IOException e) { e.printStackTrace(); }
	
		return configFile;
	}

	public String getLanguage() {
		return sLanguage;
	}
	
	public Properties getConfigFile() {
		return this.configFile;
	}
	
	public ResourceBundle getLabels() {
		Locale.setDefault(new Locale(sLanguage));
		locale = Locale.getDefault();
		return (ResourceBundle.getBundle("labels/Labels", locale));
	}
	
	public String createName(String infoSource, String[] clasCodes, String publicationDate, String title, String subtitle) {
		
		String name = "";
		
		name = getInfoSourceCode(infoSource);
		name = name + "#";
		boolean first = true;
		for(String cCode : clasCodes) {
			if(!first) name = name + "-";
			name = name + getClasCodeCode(cCode);
			first = false;
		}
		name = name + "#";
		name = name + publicationDate;
		name = name + "#";
		name = name + title;
		if(subtitle != null && !subtitle.isEmpty()){
			name = name + "###";
			name = name + subtitle;
		}
		name = name + ".pdf";
		
		return name;
	}
	
	public String getInfoSourceCode(String infoSource) {
		
		List<InfoSource> infoSources = readInfoSource();
		for(InfoSource is : infoSources) {
			if(is.getName().equalsIgnoreCase(infoSource)) return is.getCode();
		}
		return "99";
		
	}
	
	public String getClasCodeCode(String clasCode) {
		
		List<ClasCode> clasCodes = readClasCodes();
		
		for(ClasCode cc : clasCodes) {
			if((cc.getCode() + ". " + cc.getName()).equalsIgnoreCase(clasCode)) return cc.getCode();
		}
		return "99";

	}
	
public List<InfoSource> readInfoSource() {
		
		List<InfoSource> infoSources = new ArrayList<InfoSource>();

		try {
			File file = new File("infoSource.xml");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc;

			doc = db.parse(file);

			doc.getDocumentElement().normalize();
			//System.out.println("Root element " + doc.getDocumentElement().getNodeName());
			NodeList nodeLst = doc.getElementsByTagName("source");

			for (int s = 0; s < nodeLst.getLength(); s++) {

				Node fstNode = nodeLst.item(s);

				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
					
					InfoSource infoSource = new InfoSource();

					Element fstElmnt = (Element) fstNode;
					NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("code");
					Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
					NodeList fstNm = fstNmElmnt.getChildNodes();
					infoSource.setCode(((Node) fstNm.item(0)).getNodeValue());
					NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("name");
					Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
					NodeList lstNm = lstNmElmnt.getChildNodes();
					infoSource.setName(((Node) lstNm.item(0)).getNodeValue());
					
					infoSources.add(infoSource);
				}

			}

		}
		
		catch (SAXException e) { e.printStackTrace(); }
		catch (IOException e) { e.printStackTrace(); }
		catch (ParserConfigurationException e) { e.printStackTrace(); }
		
		return infoSources;
		
	}

public List<ClasCode> readClasCodes() {
	
	List<ClasCode> clasCodes = new ArrayList<ClasCode>();

	try {
		File file = new File("clasCodes.xml");
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc;

		doc = db.parse(file);

		doc.getDocumentElement().normalize();
		//System.out.println("Root element " + doc.getDocumentElement().getNodeName());
		NodeList nodeLst = doc.getElementsByTagName("cCode");

		for (int s = 0; s < nodeLst.getLength(); s++) {

			Node fstNode = nodeLst.item(s);

			if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
				
				ClasCode clasCode = new ClasCode();

				Element fstElmnt = (Element) fstNode;
				NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("code");
				Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
				NodeList fstNm = fstNmElmnt.getChildNodes();
				clasCode.setCode(((Node) fstNm.item(0)).getNodeValue());
				NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("name");
				Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
				NodeList lstNm = lstNmElmnt.getChildNodes();
				clasCode.setName(((Node) lstNm.item(0)).getNodeValue());
				
				clasCodes.add(clasCode);
			}

		}

	}
	
	catch (SAXException e) { e.printStackTrace(); }
	catch (IOException e) { e.printStackTrace(); }
	catch (ParserConfigurationException e) { e.printStackTrace(); }
	
	return clasCodes;
	
}

}
